package com.qqtech.qquan.user.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.model.UserMoneyLog;
import com.qqtech.qquan.user.model.UserMoneyLogQuery;
import com.qqtech.qquan.user.model.UserMoneyLogVo;
import com.qqtech.qquan.user.model.UserValueVo;
import com.qqtech.qquan.user.service.UserMoneyLogService;
import com.qqtech.qquan.user.service.UserValueService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/money/log")
public class UserMoneyLogController extends BaseWebController<UserMoneyLog, UserMoneyLogQuery> {
	@Resource
	private UserMoneyLogService userMoneyLogService;

	@Resource
	private UserValueService userValueService;

	@Override
	protected BaseService<UserMoneyLog> getBaseService() {
		return userMoneyLogService;
	}

	/**
	 * 分页
	 * 
	 * @param userMoneyLogQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody UserMoneyLogQuery userMoneyLogQuery) {
		Result ret = new Result();
		Timestamp searchETime = userMoneyLogQuery.getSearchETime();
		userMoneyLogQuery.setSearchETime(TimeUtil.addDays(searchETime, 1));
		Integer curUserId = super.getUserId();
		userMoneyLogQuery.setUserId(curUserId);
		Pageable pageKit = new PageKit(userMoneyLogQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserMoneyLogVo> page = userMoneyLogService.queryPageList(userMoneyLogQuery, pageKit);
		ret.put("page", page);

		UserValueVo userValueVo = userValueService.getById(curUserId);
		ret.put("cash", (userValueVo.getMoney() == null ? 0 : userValueVo.getMoney())
				+ (userValueVo.getQuanValue() == null ? 0 : userValueVo.getQuanValue()));

		ret.put("withdrawCash", userMoneyLogService.getValidMoney(curUserId));
		return ret;
	}
}
